package choreganizer;

import java.util.Calendar;
import java.util.GregorianCalendar;
import org.xml.sax.*;
import org.xml.sax.helpers.*; 

public class ChoreReader
extends DefaultHandler
{
	
	// Array of scheduled chores
	private ChoreSchedule[] choreSchedule;
	
	// Variables for getting all schedules
	private java.util.List<ChoreData> choreData;
	private boolean getAll;
	
	// Temp variables used to store info about a chore
	private String currElementName;
	private ChoreData currChoreItem;
	
	
	
	
	
	/*
	* name:		Overloaded Constructor
	* params:	boolean getAll - Do we want to read in all Schedule items
	* return:	ChoreReader
	* desc:		Should only be called when getAll is set to true, so it will
	*				create a ChoreData array to hold all possible chores
	*/
	public ChoreReader(boolean getAll)
	{
		this.getAll = getAll;
		choreData = new java.util.Vector<ChoreData>();
	}
	
	
	/*
	* name:		Overloaded Constructor
	* params:	ChoreSchedule[] choreSchedule - scheduled items we want to match chores to
	* return:	ChoreReader
	* desc:		When called, we'll search through every ScheduleData item in the
	*				ChoreSchedule array, and attach any choes we find to the item if it
	*				has a matching ID
	*/
	public ChoreReader(ChoreSchedule[] choreSchedule)
	{
		this.choreSchedule = choreSchedule;
	}
	
	
	
	
	
	
	
	
	
	
	/*
	* name:		getAllChores()
	* params:	none
	* return:	Vector<ChoreData> - collection of all chores
	* desc:		We should only call this method when we've searched
	*				for all possible chore items
	*/
	public java.util.List<ChoreData> getAllChores()
	{
		return choreData;
	}
	
	
	
	
	
	
	
	
	
	
	
	/*
	* name:		startElement()
	* params:	String namespaceURI - namespace
	*				String lName - 
	*				String qName - qualified element name
	*				Attributes - attributes for the current element
	* return:	none
	* desc:		Holds the name of an element tag, and all it's attributes
	*				We'll parse this info to create a ChoreData item
	*/
	public void startElement( String namespaceURI, String lName, String qName, Attributes attrs ) 
	throws SAXException
	{
		String eName = lName;
		if ( "".equals(eName) ) 
			eName = qName;
		
		currElementName = eName;
		
		// Reset variables on a new item
		if ( eName.equals("item") )
		{
			currChoreItem = new ChoreData();
		}
		
		// Store variables
		if (attrs != null)
		{
			for (int i=0; i < attrs.getLength(); i++)
			{
				String aVal = attrs.getValue(i).trim();
				String aName = attrs.getLocalName(i);
				if ("".equals(aName))
					aName = attrs.getQName(i);
				
				if (eName.equals("item") && aName.equals("id"))
				{
					//choreID = aVal;
					currChoreItem.setID( aVal );
				}
				
				else if (eName.equals("item") && aName.equals("dateAdded"))
				{
					//choreAdded = aVal;
					currChoreItem.setDateAdded( aVal );
				}
			}
		}
	}
	
	
	/*
	* name:		characters()
	* params:	char buf[] - array of characters read in
	*				int offset - offset from the buffer
	*				int len - number of characters in the buffer
	* return:	none
	* desc:		This will allow us to read the data in a tag
	*				We'll parse this info to create a ChoreData item
	*/
	public void characters(char buf[], int offset, int len)
	throws SAXException
	{
		
		String s = new String(buf, offset, len);
		s = s.trim();
		
		// Break out if the string is empty
		if ( s.equals("") )
			return;
		
		if ( currElementName.equals("name") )
		{
			//choreName = s;
			currChoreItem.setName( s );
		}
		
		else if ( currElementName.equals("description") )
		{
			//choreDesc = s;
			currChoreItem.setDescription( s );
		}
		
		else if ( currElementName.equals("notes") )
		{
			//choreNotes = s;
			currChoreItem.setNotes( s );
		}
	}
	
	
	/*
	* name:		endElement()
	* params:	String namespaceURI - namespace
	*				sName - tag elements name
	*				qName - qualified name
	* return:	none
	* desc:		Tells us when we've hit the end of a tag element
	*				We'll use this as a que to check and/or add the 
	*				ChoreData item we've been assembling
	*/
	public void endElement(String namespaceURI, String sName, String qName )
	throws SAXException
	{
		if ( sName.equals("item") )
		{
			if ( getAll )
			{
				choreData.add( currChoreItem );
			}
			else
			{
				for (ChoreSchedule cs: choreSchedule)
				{
					if ( currChoreItem.getID().equals(cs.getScheduleData().getChoreID()) )
					{
						cs.setChoreData( currChoreItem );
					}
				}
			}

		}
	}
	
}
